package com.easy.modules.sysmgr.web.action;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.common.util.Const;
import com.easy.common.web.BaseController;
import com.easy.common.web.page.Msg;
import com.easy.common.web.servlet.ApplicationConstants;
import com.easy.modules.sysmgr.web.form.UserSession;

@Controller
@RequestMapping("/sysmgr/monitor")
public class MonitorAction extends BaseController {

	@RequestMapping("/online_main.do")
	public String online_main() {
		return Const.SYSMGR + "online_main" ;
	}
	
	@RequestMapping("/doNotNeedAuth_getOnlineInfo.do")
	@ResponseBody
	public List<UserSession> doNotNeedAuth_getOnlineInfo() {
		List<UserSession> onlineUsers = new ArrayList<UserSession>() ;
		for(String id : ApplicationConstants.SESSION_MAP.keySet()){
			HttpSession sess = ApplicationConstants.SESSION_MAP.get(id);
			UserSession userSession = (UserSession)sess.getAttribute(Const.USER_SESSION);
			if(null != userSession) {
				userSession.setSessionId(id) ;
				userSession.setLogintime(DateFormat.getTimeInstance().format(sess.getCreationTime())) ;
				onlineUsers.add(userSession) ;
			}
		}
		return onlineUsers ;
	}
	
	@RequestMapping("/doNotNeedAuth_forceLogout.do")
	@ResponseBody
	public Msg doNotNeedAuth_forceLogout(String sessionId, HttpSession session) {
		try {
			if(null == sessionId || "".equals(sessionId)) {
				for(String id : ApplicationConstants.SESSION_MAP.keySet()){
					if(!id.equals(session.getId())) {
						ApplicationConstants.SESSION_MAP.get(id).removeAttribute(Const.USER_SESSION);
						ApplicationConstants.SESSION_MAP.remove(id) ;
					}
				}
			} else {
				String[] split = sessionId.split(",") ;
				for (int i = 0; i < split.length; i++) {
					if(!split[i].equals(session.getId())) {
						ApplicationConstants.SESSION_MAP.get(split[i]).removeAttribute(Const.USER_SESSION);
						ApplicationConstants.SESSION_MAP.remove(split[i]) ;
					}
				}
			}
		} catch (Exception e) {
			return new Msg(false, "强制用户退出发出错误！") ;
		}
		return new Msg(true) ;
	}
	
	
}
